package com.api.workflow.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.workflow.util.CreateWorkflowDetailUtil;
import com.engine.workflow.cmd.monitor.DoArchivingCmd;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.webservices.WorkflowRequestInfo;
import weaver.workflow.webservices.WorkflowServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;


@Path("/yongn/sap/oa")
public class EstablishAction {

    /**
     * 传递requestid 把流程归档
     */
    @POST
    @Path("/processArchiving")
    @Produces(MediaType.TEXT_PLAIN)
    public String processArchiving(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        // BaseBean baseBean = new BaseBean();
        Map<String, Object> apidatas = new HashMap<>();
        // 获取当前登录人的id
        /*User user = HrmUserVarify.checkUser(request, response);
        int userid = user.getUID();
        Map<String, Object> params = ParamUtil.request2Map(request);
        String requestid = String.valueOf(params.get("requestid"));
        String uid = String.valueOf(params.get("uid"));
        int requestid1 = Integer.parseInt(requestid);
        int uid1 = Integer.parseInt(uid);
        WorkflowServiceImpl ws = new WorkflowServiceImpl();
        String s = ws.doForceOver(requestid1, uid1);
        apidatas.put("result", s);*/

        // 使用流程监控中的接口进行流程归档  路径package com.engine.workflow.cmd.monitor; 方法:doArchiving
        DoArchivingCmd doArchivingCmd = new DoArchivingCmd();
        User user = HrmUserVarify.checkUser(request, response);
        apidatas = doArchivingCmd.doArchiving(request, response, user);
        return JSONObject.toJSONString(apidatas);
    }

    /*
     * 创建流程返回requestid
     */
    @POST
    @Path("/processCreation")
    @Produces(MediaType.TEXT_PLAIN)
    public String processCreation(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        // BaseBean baseBean = new BaseBean();
        Map<String, Object> apidatas = new HashMap<>();
        // 获取当前登录人的id
        User user = HrmUserVarify.checkUser(request, response);
        int uid = user.getUID();
        uid = 1;
        String workflowid = "";
        String date = request.getParameter("masterData");
        JSONObject masterDataMap = JSONObject.parseObject(date);

        workflowid = masterDataMap.get("workflowid").toString(); // 流程id
        // 获取主表数据
        JSONObject masterMap = new JSONObject();
        String lcbt = masterDataMap.get("lcbt").toString(); // 流程标题
        masterMap.put("lcbt", lcbt);
        String reqman = masterDataMap.get("reqman").toString(); // 经办人员
        masterMap.put("reqman", reqman);
        String reqdepts = masterDataMap.get("reqdept").toString(); // 经办部门
        masterMap.put("reqdepts", reqdepts);
        String reqsub = masterDataMap.get("reqsub").toString(); // 经办分部
        masterMap.put("reqsub", reqsub);
        String flowno = masterDataMap.get("flowno").toString(); // 单据编号
        masterMap.put("flowno", flowno);
        String reqdate = masterDataMap.get("reqdate").toString(); // 申请日期
        masterMap.put("reqdate", reqdate);
        String reqtel = masterDataMap.get("reqtel").toString(); // 联系方式
        masterMap.put("reqtel", reqtel);
        String sqworkflowid = masterDataMap.get("sqworkflowid").toString();// 关联事前申请
        masterMap.put("sqworkflowid", sqworkflowid);
        String isend = masterDataMap.get("isend").toString();// 是否报销完成
        masterMap.put("isend", isend);
        String eeramt = masterDataMap.get("eeramt").toString();// 报销金额
        masterMap.put("eeramt", eeramt);
        String cnpayamt = masterDataMap.get("cnpayamt").toString();// 实付金额大写
        masterMap.put("cnpayamt", cnpayamt);
        String remark = masterDataMap.get("remark").toString(); // 费用报销说明
        masterMap.put("remark", remark);

        String detailData = request.getParameter("detailData");
        JSONObject detailDatas = JSONObject.parseObject(detailData);
        String xgfp = detailDatas.get("xgfp").toString();// 相关发票(明细)
        String budgetmonth = detailDatas.get("budgetmonth").toString();// 费用所属期间(明细)
        String costcenterid = detailDatas.get("costcenterid").toString();// 费用承担(明细)
        String budgetaccountid = detailDatas.get("budgetaccountid").toString();// 预算科目(明细)
        String ablebudgetamt = detailDatas.get("ablebudgetamt").toString();// 预算信息(明细)
        String expenseamt = detailDatas.get("expenseamt").toString();// 费用金额(明细)
        String actacteeramt = detailDatas.get("actacteeramt").toString();// 实报金额(明细)
        JSONArray detailList = new JSONArray();
        detailList.add(new JSONObject().fluentPut("xgfp", xgfp));// 相关发票(明细)
        detailList.add(new JSONObject().fluentPut("budgetmonth", budgetmonth));// 费用所属期间(明细)
        detailList.add(new JSONObject().fluentPut("costcenterid", costcenterid));// 费用承担(明细)
        detailList.add(new JSONObject().fluentPut("budgetaccountid", budgetaccountid));// 预算科目(明细)
        detailList.add(new JSONObject().fluentPut("ablebudgetamt", ablebudgetamt));// 预算信息(明细)
        detailList.add(new JSONObject().fluentPut("expenseamt", expenseamt));// 费用金额(明细)
        detailList.add(new JSONObject().fluentPut("actacteeramt", actacteeramt));// 实报金额(明细)

        String masterMapJson = masterMap.toString();
        String detailListJson = detailList.toString();
        String workflowDetail = CreateWorkflowDetailUtil.createWorkflowDetail(lcbt, workflowid, String.valueOf(uid), masterMapJson, detailListJson);
        apidatas.put("workflowDetail", workflowDetail);
        AutoSubmitV0006(workflowDetail, String.valueOf(uid));// 自动提交流程至下一节点
        return JSONObject.toJSONString(apidatas);
    }
    /**
     * 提交流程
     *
     * @param requestid 流程号
     * @param userid    用户id
     * @return
     */
    public String AutoSubmitV0006(String requestid, String userid) {
        WorkflowServiceImpl ws = new WorkflowServiceImpl();
        WorkflowRequestInfo wri = new WorkflowRequestInfo();
        String result = ws.submitWorkflowRequest(wri,
                Integer.valueOf(requestid), Integer.valueOf(userid), "submit",
                "自动提交");
        return result;
    }
}
